package Gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import DB.PhraseConnector;

@SuppressWarnings("serial")
public class MoreInfoFrame extends JFrame {

	private JPanel contentPane;

	/**
	 * Create the frame.
	 * @param blurb2 
	 */
	public MoreInfoFrame(String exp, final String id, final String blurb, final String player) {		
		setTitle("Wheel Of Fortune");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 511, 489);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JTextPane txtTitle = new JTextPane();
		txtTitle.setEditable(false);
		txtTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtTitle.setBackground(UIManager.getColor("Button.background"));
		txtTitle.setText(exp);
		txtTitle.setBounds(63, 30, 97, 20);
		contentPane.add(txtTitle);


		int w=0, h=0;
		try {
			InputStream is = PhraseConnector.getInstance().getPicture(id);
			Image image = ImageIO.read(is);
			is.close();
			h = image.getHeight(null);
			w = image.getWidth(null);
			//frame = new JFrame();
			JLabel label = new JLabel(new ImageIcon(image));
			label.setBounds(63, 60, w, h);
			contentPane.add(label);

			final JTextArea txt = new JTextArea(blurb);
			txt.setLineWrap(true);
			txt.setBackground(Color.LIGHT_GRAY);
			txt.setBounds(63, 70+10+h, 354, 212);
			contentPane.add(txt);

			JButton btnCloseSaveChanges = new JButton("Close and save");
			btnCloseSaveChanges.setBounds(349, 11, 144, 23);
			contentPane.add(btnCloseSaveChanges);
			btnCloseSaveChanges.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (!blurb.equals(txt.getText())){
						PhraseConnector.getInstance().putArticle(id, txt.getText(), player);
					}
					setVisible(false);
				}
			});


			JButton btnCloseDontSave = new JButton("Close without saving");
			btnCloseDontSave.setBounds(349, 49, 144, 23);
			contentPane.add(btnCloseDontSave);
			btnCloseDontSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setVisible(false);
				}
			});
		}
		catch (Exception e){}
	}
}